<?php

namespace App\Admin\Eloquent;

class Privileges extends Model
{
    /**
     * 实例化模型时, 可批量赋值属性白名单
     *
     * @var array
     */
    protected $fillable = [
        'role_id', 'resource_id','resource_type','description',
    ];

    /*
     * 储存角色权限
     */
    public static function saveResource($role_id,$cate,$data){
        $res=Privileges::where('role_id',$role_id)
            ->where('resource_type',$cate)
            ->first();
        if(empty($res)){
            $privileges=new Privileges();
        }
        else{
            $privileges=Privileges::find($res->id);
        }
        $privileges->role_id=$role_id;
        $privileges->resource_type=$cate;
        $privileges->resource_id=$data;
        $privileges->save();
        return true;
    }
    /*
     * 查询角色权限
     */
    public static function getResource($id){
        $res=Privileges::where('role_id','=',$id)
            ->value('resource_id');
        return $res;
    }
}